package jtg.util;

import org.apache.commons.lang3.RandomStringUtils;

import java.util.Objects;
import java.util.Random;

public class RandomUtil {

    private static Random random = new Random();

    //-------------------------------RandomInt begin-------------------------------
    /**随机生成整数
     * */
    public static int randInt(){
        return random.nextInt();
    }
    /**随机生成整数
     * 给出上界
     * */
    public static int randInt(int bound) {
        return random.nextInt(bound);
    }
    /**随机生成整数
     * 给出下界
     * */
    public static int randIntWithLowerBound(int lowerBound) {
        return random.nextInt(lowerBound);
    }

    /**随机生成整数
     * 给出上界和下界
     * */
    public static int randInt(int lowerBound, int upperBound){
        assert lowerBound<=upperBound : "error:The upper bound should be less than the lower bound";
        return random.nextInt(upperBound-lowerBound)+lowerBound;
    }
    //-------------------------------RandomInt end-------------------------------

    //-------------------------------RandomDouble begin-------------------------------
    public static double randDouble(){
        return random.nextDouble();
    }

    public static char randChar(){
        return (char) random.nextInt(65536);
    }

    public static boolean randBool(){
        return random.nextBoolean();
    }

    public static String randStr(){
        return RandomStringUtils.randomAlphanumeric(randInt(30));
    }

    public static String randStr(int len){
        return RandomStringUtils.randomAlphanumeric(len);
    }

    /**
     * @param headFlag 生成的字符串以headFlag开头
     * @param sources 用于生成的字符集，如果sources=""||null ==> sources 为默认的。
     * @param len 生成的字符串长度，不包括headFlag；如果len<0,则默认len=6
     */
    public static String randStr(String headFlag , String sources, int len){
        if (Objects.equals(sources, "")||sources==null){
            sources = StaticsUtil.sources1;
        }
        if (len<0)
            len = StaticsUtil.DEFAULT_STRING_LENGTH;
        StringBuilder result = new StringBuilder(); //定义一个边长的字符串对象
        result.append(headFlag);
        Random rand = new Random();

        for (int j = 0; j < len; j++)
        {
            result.append(sources.charAt(rand.nextInt(sources.length())));
        }
        return result.toString();
    }
    //---------------------test------------------------
    public static void main(String[] args) {
        for (int i = 0;i<20;i++)
        {
            System.out.println(randStr("#####","",2));
        }
    }

}
